<?php
namespace Home\Controller;

use Common\Model\UserModel;
use Common\Util\BaseData;
use Common\Util\Wxrcode;
use Services\Log\LogService;
use Services\WxGongZhong\WxGongZhongService;

class IndexController{

	public function cancelVipStatus(){
		$where['is_vip'] = 1;
		$where['vip_expired_at'] = ['lt', get_now_date()];
		$user_m = new UserModel();
		$cancel = $user_m->where($where)->limit(200)->select();
		foreach($cancel as $key => $val){
			//	更新自己为普通用户，取消订阅提醒，设置为关闭谁看我提醒
			$cancelAction = $user_m->where(['id' => $val['id']])->save([
//				'view_log_is_tip'   => 0,
//				'view_log_tip_type' => 0,
				'is_vip'            => 0,
				'update_time'       => time(),
				'vip_expired_at'    => null,
			]);
			if($cancelAction){
				LogService::writeWithDb("用户ID：".$val['id'].";更新时间".date("Y-m-d H:i:s"), 'cancelVipStatus', '取消VIP状态');
			}
		}
	}

	//	发送模版消息
	public function sendTemplet(){
		$appSetting = BaseData::getAppSetting();
//		$where['is_vip'] = 1;
		$where['view_log_tip_type'] = ['gt', 1];
		$where['log_tip_time'] = ['lt', time()];
		$user_m = new UserModel();
		$cancel = $user_m->where($where)->limit(50)->order("log_tip_time asc")->select();
		$config = [
			'appid' => arr_get($appSetting,'wechat_appid'),
			'secret' => arr_get($appSetting,'wechat_appsecret'),
		];
		$service = new WxGongZhongService($config);
		foreach($cancel as $key => $val){
			//			统计看我的多少人，多少次
			$tip_count = M("user_view_log")->where(['view_user_id' => $val['id'], 'is_tip' => 0])->count();
			if ($tip_count) {
				$service->sendViewMessage($val['id'], $tip_count);
			}
		}

		LogService::writeWithDb('发送模版消息脚本执行完毕', 'sendTemplet','访客提醒脚本扫描');
		echo 'success';
	}
}

?>
